import axios from "axios";

import { getToken, delToken } from "./storage";

import { Message } from "element-ui";

const request = axios.create({
  baseURL: "https://lianghj.top:8888/api/private/v1/",
  timeout: 5000,
});

//响应拦截器
request.interceptors.request.use((config) => {
  const token = getToken();
  if (token) {
    config.headers.Authorization = token;
  }
  return config;
});

// 请求拦截器
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (err) => {
    console.log(err);
    if (err.response.status === 401) {
      delToken();
      this.$router.push("/login");
      Message.error("登录过期，请重新登录");
    } else {
      Message.error(err?.response?.data?.message || "系统错误");
    }
    return Promise.reject(err);
  }
);
export default request;
